/**
 * Created by zhaocong on 2016/6/24.
 */

var cc = window.cc || {};

cc.program = new function () {
  'use strict';
  var self = this;

  self.input = $('#search'); // 搜索框
  self.casebtn = $('#case'); // 搜索按钮
  self.dlist = $('#datalist'); // 搜索提示框
  self.scontent = $('.search-content'); // 搜索框
  self.result = $('.result'); // 搜索结果
  self.ewm = $('#ewm'); // 二维码

  self._stLabel = ''; // 保存用户选择的股票

  /**
   * 数据轴
   * @param ele
   */
  self.line = function (ele) {
    $(ele).each(function () {
      var size = $(this).data('size');
      $(this).find('.pop').html(size).css('left',size);
      $(this).find('.line-l').css('width',size);
      if(parseInt(size)>=50){
        $(this).find('.line-l').addClass('red');
      }
    })
  };

  /**
   * 搜索返回效果
   */
  self.search = function () {
    self.casebtn.on('click',self.searchevent);
    self.input.on('keyup',self.filter);
    self.datalist();
  };

  self.searchevent = function () {
    var txt = $.trim(self.input.val());
    if(txt){
      if((/[\u4e00-\u9fa5]+/).test(txt)){
        if(self._stLabel){
          self.sub(self._stLabel);
        }else{
          self.sub(self.dlist.find('li').eq(0).find('.data-l').html());
        }
      }else{
        if($('#datalist li').length){
          self.sub(self.dlist.find('li').eq(0).find('.data-l').html());
        }else{
          self.sub(txt);
        }
      }
    }
  };

  /**
   * 筛选器
   */
  self.filter = function (e) {
    var txt = $.trim(self.input.val());
    var data = {};
    if(event.keyCode == 13){
      self.searchevent()
    }else{
      if(txt){
        data.q = txt;
        $.ajax({
          type: 'get',
          // url: 'http://www.chinawisecom.net/weixin/risk/stockconfig/filterstklabel',
          url: '../php/dedata.json',
          data: data,
          async: false,
          beforeSend: function () {
          },
          success: function (data) {
            //console.log(data);
            self.filterinfo(data);
            self.dlist.fadeIn();
          },
          error: function () {
          }
        });
      }
    }
  };

  /**
   * 呈现筛选结果
   * @param data
   */
  self.filterinfo = function (data) {
    var box = self.dlist,
        _html = '<li><p class="data-l">__stkLabel__</p><p class="data-r">__stkName__</p></li>',
        htmllist = [];
    $.each(data.rows,function (i,el) {
        htmllist.push(
            _html.replace(/__stkLabel__/,el.stkLabel)
                 .replace(/__stkName__/,el.stkName)
        );
    });
    box.html(htmllist.join(''));
  };

  /**
   * 筛选匡
   */
  self.datalist = function () {
    self.dlist.on('click','li',function () {
      self.input.val($(this).find('.data-r').html()).focus();
      self._stLabel = $(this).find('.data-l').html();
    });
    $(window).click(function () {
      self.dlist.hide();
    });
  };

  /**
   * 提交信息
   * @param txt
   */
  self.sub = function (txt) {
    var data = {};
    data.stklabel = txt;
    $.ajax({
      type: 'get',
      // url: 'http://www.chinawisecom.net/weixin/wise/getOneStockDiagnosis',
      url: '../php/data.json',
      data: data,
      async: false,
      beforeSend: function () {
      },
      success: function (data) {
        $('.com-header .erro').remove();
        self.scontent.addClass('min-header');
        self.result.fadeIn();
        self.ewm.hide();

        self.addinfo(data);
        self.storage();
        self.dlist.hide();

        window.localStorage.setItem('c',(new Date).toLocaleDateString());
      },
      error: function () {
        $('.com-header .erro').remove();
        $('.com-header').append('<div class="erro">未找到该股票，请选择股票后诊断。</div>')
      }
    });
  };

  /**
   * 添加信息到页面
   */
  self.addinfo = function (data) {
    $('#stLabel').html(data.stLabel);
    $('#stName').html(data.stName);
    $('#sv').data('size',parseInt(parseFloat(data.sv)*100)+'%');
    $('#mv').data('size',parseInt(parseFloat(data.mv)*100)+'%');
    $('#lv').data('size',parseInt(parseFloat(data.lv)*100)+'%');

    self.line('.line-box');
  };

  /**
   * 是否有搜索过
   */
  self.storage = function () {
    if(window.localStorage.getItem('c') == (new Date).toLocaleDateString()){
      self._cate();
    }
  };
  self._cate = function () {
    self.scontent.addClass('min-header');
    self.result.hide();
    self.ewm.show();
    self.input.off('keyup');
    self.casebtn.off('click');
  };

  /**
   * 清空存储 ❤
   */
  self.clear = function () {
    localStorage.clear();
  };

  /**
   *  轮播图
   */
  self.swiper = function () {
    var swiper = swiper || new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      paginationClickable: '.swiper-pagination',
      autoplay: 4000,
      spaceBetween: 30
    });
  };

  /**
   * 显示弹窗
   */
  self.attention = function () {
    $('#attention').click(function () {
      $('.pop-s').show();
      self.swiper();
    });
    $('.close,.pop-mask').click(function () {
      $('.pop-s').hide();
    });
  };

  /**
   * 拷贝文字
   */
  self.copy = function () {
    var clipboard = new Clipboard($('#attention')[0]);
    clipboard.on('success', function(e) {
      console.log('复制成功');
    });
    clipboard.on('error', function(e) {
      console.log(e);
    });
  }
};
